package com.osmino.contacts.list;

import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.entity.Email;
import com.osmino.contacts.entity.Group;
import com.osmino.contacts.entity.Phone;

public final class ContactViewAdapter extends BaseAdapter {

	public static final int HEADER_ITEM_TYPE = 0;
	public static final int PHONE_ITEM_TYPE = 1;
	public static final int EMAIL_ITEM_TYPE = 2;
	public static final int GROUP_ITEM_TYPE = 3;
	public static final int RINGTONE_ITEM_TYPE = 4;
	private final Context mContext;
	private final IContactViewController mController;
	private Contact mContact;
	private Map<Long, Group> mMapGroups;

	public ContactViewAdapter(Context context, IContactViewController controller) {
		mContext = context;
		mController = controller;
	}

	@Override
	public int getCount() {
		int count = 0;
		if (mContact != null) {
			count = mContact.getPhones().size() + mContact.getEmails().size() + 3;
		}
		return count;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getViewTypeCount() {
		return 5;
	}

	@Override
	public int getItemViewType(int position) {
		int type;
		int sizePhoneEmailList = mContact.getPhones().size() + mContact.getEmails().size();
		if (position == 0) {
			type = HEADER_ITEM_TYPE;
		} else if (position <= mContact.getPhones().size()) {
			type = PHONE_ITEM_TYPE;
		} else if (position <= sizePhoneEmailList) {
			type = EMAIL_ITEM_TYPE;
		} else {
			if (position == sizePhoneEmailList + 1) {
				type = GROUP_ITEM_TYPE;
			} else {
				type = RINGTONE_ITEM_TYPE;
			}
		}
		return type;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		int viewType = getItemViewType(position);
		return (viewType == GROUP_ITEM_TYPE) || (viewType == RINGTONE_ITEM_TYPE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		if (convertView == null) {
			item = newView(position, parent);
		} else {
			item = convertView;
		}
		bindView(position, item);
		return item;
	}

	private View newView(int position, ViewGroup parent) {
		int viewType = getItemViewType(position);
		View item = null;
		switch (viewType) {
			case HEADER_ITEM_TYPE:
				item = new ContactViewHeadItem(mContext, mController);
				break;
			case PHONE_ITEM_TYPE:
				item = new ContactViewPhoneItem(mContext, mController);
				break;
			case EMAIL_ITEM_TYPE:
				item = new ContactViewEmailItem(mContext, mController);
				break;
			case GROUP_ITEM_TYPE:
				item = new ContactViewGroupItem(mContext, mController);
				break;
			case RINGTONE_ITEM_TYPE:
				item = new ContactViewRingtoneItem(mContext, mController);
				break;
		}
		return item;
	}

	private void bindView(int position, View view) {
		int viewType = getItemViewType(position);
		switch (viewType) {
			case HEADER_ITEM_TYPE:
				((ContactViewHeadItem) view).setContact(mContact);
				break;
			case PHONE_ITEM_TYPE:
				Phone phone = mContact.getPhones().get(position - 1);
				((ContactViewPhoneItem) view).setPhone(phone);
				break;
			case EMAIL_ITEM_TYPE:
				Email email = mContact.getEmails().get(position - 1 - mContact.getPhones().size());
				((ContactViewEmailItem) view).setEmail(email);
				break;
			case GROUP_ITEM_TYPE:
				List<Long> groupIds = mContact.getGroupIds();
				((ContactViewGroupItem) view).setGroups(mMapGroups, groupIds);
				break;
			case RINGTONE_ITEM_TYPE:
				((ContactViewRingtoneItem) view).setRingtone(mContact.ringtone);
				break;
		}
	}

	public void setContact(Contact contact, Map<Long, Group> mapGroups) {
		mContact = contact;
		mMapGroups = mapGroups;
		notifyDataSetChanged();
	}

}
